package com.rljk.controller.app;

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.CaidanManager;
import com.rljk.service.TaskManager;
import com.rljk.service.UserManager;
import com.rljk.service.impl.*;
import com.rljk.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:   YASUO
 * Date:     2018/6/26 17:27
 * Description: 任务接口
 */
@RestController
@RequestMapping("/task")
@Slf4j
@CacheConfig(cacheNames = "task")
public class TaskInController extends BaseController{

    @Resource(name = "taskService")
    private TaskManager taskService;

    @Resource(name = "caidanService")
    private CaidanManager caidanService;

    @Resource(name = "userService")
    private UserManager userService;

    @Autowired
    RedisService redisService;

    @Resource(name = "candyService")
    private CandyService candyService;

    @Resource(name = "rewardService")
    private RewardService rewardService;

    @Resource(name = "assetsService")
    private AssetsService assetsService;

    @Resource(name = "projectlineService")
    private ProjectLineService projectLineService;

    @Resource(name = "projectsurveyService")
    private ProjectSurveyService projectSurveyService;

    @Resource(name = "projectbasicService")
    private ProjectBasicService projectBasicService;

    @Resource(name = "questiondetailService")
    private QuestionDetailService questionDetailService;

    @Resource(name = "questionsubmitService")
    private QuestionSubmitService questionSubmitService;

    /**
     * 任务页面显示接口
     * author:YASUO
     * date:2018/6/26
     */
    @RequestMapping(value="/showview")
    //@Cacheable(key = "987654")
    public Object showview() {
        log.info("任务页面显示接口");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("TASKSHOWVIEW", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("useridParam",pd)){
                    //1.获取用户信息
                    String token = redisService.get(pd.getString("TOKEN"));
                    if(Tools.isEmpty(token)){
                        return Result.error(CodeMsg.TOKEN_ERROR);
                    }
                    pd.put("USER_ID",token);
                    PageData user = userService.findById(pd);

                    pd.put("CAIDAN_TYPE","2");//1-代表首页菜单，2代表任务页面菜单
                    List<PageData> caidan = caidanService.listAll(pd);
                    for (PageData p : caidan) {
                       p.remove("SEQUENCE");
                       p.remove("CAIDAN_TYPE");
                    }
                    if(Tools.isEmpty(pd.getString("CAIDAN_ID"))){
                        pd.put("CAIDAN_ID","461203874594684928");
                    }
                    List<PageData> taskList = taskService.listAll(pd);
                    for (PageData p : taskList) {
                        if(Tools.isEmpty(user.getString("LONGITUDE"))){
                            p.put("DISTANCE","未知");
                        }else {
                            String distance = MapDistance.getDistance(user.getString("LONGITUDE"), user.getString("LATITUDE"),
                                    p.getString("LONGITUDE"), p.getString("LATITUDE"));
                            p.put("DISTANCE",MapDistance.getOne(Double.parseDouble(distance)));
                        }
                        p.remove("ACCEPT_USER_ID");
                        p.remove("STATUS");
                        p.remove("LONGITUDE");
                        p.remove("LATITUDE");
                        p.remove("CREATE_TIME");
                        p.remove("UPDATE_TIME");
                        p.put("EXPIRE_TIME", RelativeDateFormat.formatLate(p.getString("EXPIRE_TIME")));

                    }
                    map.put("caidan",caidan);
                    map.put("taskList",taskList);
                    return Result.success(map);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * app任务中心获取我发布的和我接受的任务
     * author:YASUO
     * date:2018/6/26
     */
    @RequestMapping(value="/taskcenter")
    public Object getTaskCenter() {
        log.info("app任务中心获取我发布的和我接受的任务");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("TASKCENTER", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getTaskCenter",pd)){
                    //1.获取用户信息
                    String USER_ID = redisService.get(pd.getString("TOKEN"));
                    pd.put("USER_ID",USER_ID);
                    PageData user = userService.findById(pd);
                    if(user==null){
                        return Result.error(CodeMsg.EMPTY);
                    }

                    if (pd.getString("TASK_TYPE").equals("0")){//我发布的
                        pd.put("PUBLISH_USER_ID",user.get("USER_ID"));
                    }if (pd.getString("TASK_TYPE").equals("1")){//我接受的
                        pd.put("ACCEPT_USER_ID",user.get("USER_ID"));
                    }

                    List<PageData> list = taskService.getTaskCenter(pd);
                    for (PageData p : list) {
                        String distance = MapDistance.getDistance(user.getString("LONGITUDE"), user.getString("LATITUDE"),
                                p.getString("LONGITUDE"), p.getString("LATITUDE"));
                        p.remove("ACCEPT_USER_ID");
                        p.remove("STATUS");
                        p.remove("LONGITUDE");
                        p.remove("LATITUDE");
                        p.remove("CREATE_TIME");
                        p.remove("UPDATE_TIME");
                        p.put("EXPIRE_TIME", RelativeDateFormat.formatLate(p.getString("EXPIRE_TIME")));
                        p.put("DISTANCE",MapDistance.getOne(Double.parseDouble(distance)));
                    }
                    map.put("list",list);
                    return Result.success(map);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * app发布任务
     * author:YASUO
     * date:2018/7/18
     */
    @PostMapping(value="/publishtask")
    public Object addTask() {
        log.info("app发布任务");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("ADDTASK", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("addTask",pd)){
                    //1.获取用户信息
                    String USER_ID = redisService.get(pd.getString("TOKEN"));
                    pd.put("USER_ID",USER_ID);
                    pd.put("TASK_ID",this.getSnowId());
                    pd.put("PUBLISH_USER_ID",pd.get("USER_ID"));
                    pd.put("TITLE","贝爷");
                    pd.put("MAIN_IMAGE","http://image.rongyaohk.com/gemstone/task/task_image_1@2x.png");
                    pd.put("CITY","深圳");
                    pd.put("EXPIRE_TIME","2018-07-30 21:26:22");
                    pd.put("CAIDAN_ID","461203874594684928");
                    pd.put("LONGITUDE","116.597805");
                    pd.put("LATITUDE","36.738024");
                    taskService.save(pd);
                    return Result.success();
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * app获取糖果任务
     * author:YASUO
     * date:2018/8/25
     */
    @RequestMapping(value="/getcandylist")
    public Object getCandyList() {
        log.info("app获取糖果任务");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("CANDYLIST", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("useridParam",pd)){
                    List<PageData> candyList = candyService.getCandyList();
                    if(Tools.isEmptyList(candyList)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    return Result.success(candyList);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * app获取悬赏任务
     * author:YASUO
     * date:2018/8/25
     */
    @RequestMapping(value="/getrewardlist")
    public Object getRewardList() {
        log.info("app获取悬赏任务");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("REWARD", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("useridParam",pd)){
                    List<PageData> rewardList = rewardService.getRewardList(pd);
                    if(Tools.isEmptyList(rewardList)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    return Result.success(rewardList);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * app获取问卷任务
     * author:YASUO
     * date:2018/8/25
     */
    @RequestMapping(value="/getquestionlist")
    public Object getQuestionList() {
        log.info("app获取问卷任务");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("QUESTION", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("useridParam",pd)){
                    List<PageData> questionList = rewardService.getQuestionList(pd);
                    if(Tools.isEmptyList(questionList)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    return Result.success(questionList);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * app任务糖果兑换1
     * author:YASUO
     * date:2018/8/27
     */
    @RequestMapping(value="/candyexchange")
    public Object candyExchange() {
        log.info("app任务糖果兑换1");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("CANDYEXCHANGE", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("useridParam",pd)){
                    String USER_ID = redisService.get(pd.getString("TOKEN"));
                    pd.put("USER_ID",USER_ID);
                    PageData asserts = assetsService.findById(pd);
                    map.put("name1","消耗XW链数量");
                    map.put("XW_NUMBER",100);
                    map.put("name2","XW链余额："+asserts.get("XW_VALUE")+"个");
                    map.put("basic","基本信息");
                    map.put("projectsurvey","项目概况");
                    map.put("projectline","路线图");
                    return Result.success(map);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 根据项目库id获取项目概况
     * author:YASUO
     * date:2018/8/27
     */
    @RequestMapping(value="/getprojectsurvey")
    public Object getProjectSurveyById() {
        log.info("根据项目库id获取项目概况");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("PROJECTSURVEY", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getProjectSurveyById",pd)){
                    List<PageData> list = projectSurveyService.getProjectSurveyById(pd);
                    if(Tools.isEmptyList(list)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    return Result.success(list);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * 根据项目库id获取线路图
     * author:YASUO
     * date:2018/8/27
     */
    @RequestMapping(value="/getprojectline")
    public Object getProjectLineById() {
        log.info("根据项目库id获取线路图");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("PROJECTLINE", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getProjectSurveyById",pd)){
                    List<PageData> list = projectLineService.getProjectLineById(pd);
                    if(Tools.isEmptyList(list)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    return Result.success(list);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * 根据项目库id获取项目基本信息
     * author:YASUO
     * date:2018/8/27
     */
    @RequestMapping(value="/getprojectbasuc")
    public Object getProjectBasicById() {
        log.info("根据项目库id获取项目基本信息");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("PROJECTBASIC", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getProjectSurveyById",pd)){
                    PageData basic = projectBasicService.getProjectBasicById(pd);
                    List<Object> list = new ArrayList<>();


                    Map<String,Object> map1 = new HashMap<>();
                    map1.put("name","项目名称");
                    map1.put("content",basic.get("NAME"));
                    list.add(map1);

                    Map<String,Object> map2 = new HashMap<>();
                    map2.put("name","项目简称");
                    map2.put("content",basic.get("BRIEF"));
                    list.add(map2);

                    Map<String,Object> map3 = new HashMap<>();
                    map3.put("name","项目概念");
                    map3.put("content",basic.get("CONCEPT"));
                    list.add(map3);

                    Map<String,Object> map4 = new HashMap<>();
                    map4.put("name","发行总量");
                    map4.put("content",basic.get("PUBLISH_TOTAL"));
                    list.add(map4);

                    Map<String,Object> map5 = new HashMap<>();
                    map5.put("name","上线时间");
                    map5.put("content",basic.get("ONLINE_TIME"));
                    list.add(map5);

                    Map<String,Object> map6 = new HashMap<>();
                    map6.put("name","团队人数");
                    map6.put("content",basic.get("TEAM_PEOPLE"));
                    list.add(map6);

                    Map<String,Object> map7 = new HashMap<>();
                    map7.put("name","国家");
                    map7.put("content",basic.get("COUNTRY"));
                    list.add(map7);

                    Map<String,Object> map8 = new HashMap<>();
                    map8.put("name","官方网址");
                    map8.put("content",basic.get("OFFICIAL_URL"));
                    list.add(map8);

                    Map<String,Object> map9 = new HashMap<>();
                    map9.put("name","查看白皮书");
                    map9.put("content","");
                    list.add(map9);
                    return Result.success(list);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 通过ID获取问卷详情列表
     * author:YASUO
     * date:2018/8/31
     */
    @RequestMapping(value="/getquesdetailbyid")
    public Object getQuesDetailById() {
        log.info("通过ID获取问卷详情列表");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("QUESTIONDATAILBYID", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getQuesDetailById",pd)){
                    List<PageData> list = questionDetailService.getQuesDetailById(pd.getString("REWARD_ID"));
                    if(Tools.isEmptyList(list)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    return Result.success(list);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 用户提交问卷列表
     * author:YASUO
     * date:2018/8/31
     */
    @RequestMapping(value="/addquesdetail")
    public Object addQuesDetail() {
        log.info("用户提交问卷列表");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("ADDQUESTIONDATAIL", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("addQuesDetail",pd)){
                    String USER_ID =(String)redisService.getUserIdByToken(pd.getString("TOKEN"));
                    pd.put("USER_ID",USER_ID);
                    pd.put("QUESTIONSUBMIT_ID",SnowIdUtil.getSnowId());
                    PageData byId = questionSubmitService.findById(pd);
                    if(byId!=null){
                        return Result.error(CodeMsg.SUBMIT_REPEAT);
                    }
                    questionSubmitService.save(pd);
                    return Result.success();
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }








}
